package com.paic.mhis.hcpms.quota.settlement.biz.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.biz.service.KpiCoreDataService;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiConditionDTO;
import com.paic.mhis.hcpms.quota.calculationreport.kpiinterface.dto.KpiSociationDTO;
import com.paic.mhis.hcpms.quota.settlement.biz.service.FeeExecuteInfoService;
import com.paic.mhis.hcpms.quota.settlement.common.Constants;

@Service("feeExecuteInfoService")
public class FeeExecuteInfoServiceImpl implements FeeExecuteInfoService {
	// @Autowired
	// private CommonDAO commonDao;
	// @Autowired
	// private DoctorInfoDAO doctorInfoDAO;
	@Autowired
	private KpiCoreDataService kpiCoreDataService;

	@Override
	public Map<String, Object> getFeeInfo(String startDate, String endDate, String type, String regionCode,
			String kpiKey, String visitType) {

		KpiConditionDTO conditionDTO = new KpiConditionDTO();
		List<String> kpiCodeList = new ArrayList<String>();
		kpiCodeList.add("KPI0001");// 费用总额
		kpiCodeList.add("KPI0003");// 预拨付
		conditionDTO.setKpiCodeList(kpiCodeList);
		conditionDTO.setSociationCode(regionCode);
		conditionDTO.setVisitType("0");
		conditionDTO.setPeriod("13");
		conditionDTO.setKpiTimeStart(startDate);
		conditionDTO.setKpiTimeEnd(endDate);
		String visitTypeParam = Constants.visitType_all_value;
		if (Constants.visitType_hosp.equals(visitType)) {
			visitTypeParam = Constants.visitType_hosp_value;
		} else if (Constants.visitType_out.equals(visitType)) {
			visitTypeParam = Constants.visitType_out_value;
		} else {
			visitTypeParam = Constants.visitType_all_value;
		}
		conditionDTO.setVisitType(visitTypeParam);
		List<KpiSociationDTO> dataList = kpiCoreDataService.queryKpiBySoc(conditionDTO);
		String kpiCode = "";
		String kpiTime = "";
		BigDecimal kpiValue = null;
		BigDecimal totalPay = new BigDecimal(0);
		BigDecimal preFeeTot = new BigDecimal(0);
		BigDecimal periodRatio = null;// 同比
		BigDecimal chainRatio = null;// 环比
		Map<String, Integer> orderMap = createOrderList(startDate, endDate);
		List<BigDecimal> periodList = createSetListValue(orderMap.size(), new BigDecimal(0));
		List<BigDecimal> chainList = createSetListValue(orderMap.size(), new BigDecimal(0));
		List<BigDecimal> perMonthList = createSetListValue(orderMap.size(), new BigDecimal(0));
		// 总额
		List<BigDecimal> tatalPayList = createSetListValue(orderMap.size(), new BigDecimal(0));
		// 每月预拨的同比
		List<BigDecimal> perMonthPeriodList = createSetListValue(orderMap.size(), new BigDecimal(0));
		// 每月预拨的环比
		List<BigDecimal> perMonthChainList = createSetListValue(orderMap.size(), new BigDecimal(0));
		for (KpiSociationDTO kpiSociationDTO : dataList) {
			kpiCode = kpiSociationDTO.getKpiCode();
			kpiValue = kpiSociationDTO.getKpiValue();
			periodRatio = kpiSociationDTO.getPeriodRatio();
			chainRatio = kpiSociationDTO.getChainRatio();
			kpiTime = kpiSociationDTO.getKpiTime();
			// 医疗总额
			if (kpiCode.equals("KPI0001")) {
				totalPay = totalPay.add(filterNullValue(kpiValue));
				periodList.set(orderMap.get(kpiTime), filterNullValue(periodRatio));
				chainList.set(orderMap.get(kpiTime), filterNullValue(chainRatio));
				tatalPayList.set(orderMap.get(kpiTime), filterNullValue(kpiValue));
			}
			// 预拨付
			if (kpiCode.equals("KPI0003")) {
				preFeeTot = preFeeTot.add(kpiValue);
				perMonthList.set(orderMap.get(kpiTime), filterNullValue(kpiValue));
				perMonthPeriodList.set(orderMap.get(kpiTime), filterNullValue(periodRatio));
				perMonthChainList.set(orderMap.get(kpiTime), filterNullValue(chainRatio));
			}
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("totalPay", yuanToBillion(totalPay));
		resultMap.put("preFeeTot", yuanToBillion(preFeeTot));
		KpiSociationDTO dto = getCurMonthVal(regionCode, endDate, "13", visitTypeParam, "KPI0001");
		KpiSociationDTO perMonthDto = getCurMonthVal(regionCode, endDate, "13", visitTypeParam, "KPI0003");
		// 医疗总费用
		if (null != dto) {
			resultMap.put("curMonthVal", yuanToBillion(dto.getKpiValue()));// 当月值
			resultMap.put("compChain", dto.getChainRatio());// 环比
			resultMap.put("compSamePeriod", dto.getPeriodRatio());// 同比
			resultMap.put("curMonthValUnit", dto.getKpiUnit());// 单位
		} else {
			resultMap.put("curMonthVal", "0.00");// 当月值
			resultMap.put("compChain", "0.00");// 环比
			resultMap.put("compSamePeriod", "0.00");// 同比
			resultMap.put("curMonthValUnit", "");// 单位
		}
		// 预拨的当月值同比环比
		if (null != perMonthDto) {
			resultMap.put("perMonthCurMonthVal", yuanToBillion(perMonthDto.getKpiValue()));// 当月值
			resultMap.put("perMonthCompChain", perMonthDto.getChainRatio());// 环比
			resultMap.put("perMonthCompSamePeriod", perMonthDto.getPeriodRatio());// 同比
			resultMap.put("perMonthCurMonthValUnit", perMonthDto.getKpiUnit());// 单位
		} else {
			resultMap.put("perMonthCurMonthVal", "0.00");// 当月值
			resultMap.put("perMonthCompChain", "0.00");// 环比
			resultMap.put("perMonthCompSamePeriod", "0.00");// 同比
			resultMap.put("perMonthCurMonthValUnit", "");// 单位
		}

		// feeInfoVO.set
		if (!type.equals("1")) {
			resultMap.put("perMonthData", totalList(perMonthList));
			resultMap.put("periodData", periodList);
			resultMap.put("chainData", chainList);
			resultMap.put("totalPayData", totalList(tatalPayList));// 总额列表
			resultMap.put("perMonthPeriodData", perMonthPeriodList);// 预拨同比列表
			resultMap.put("perMonthChainData", perMonthChainList);// 预拨环比列表

		} else {
			resultMap.put("perMonthData", toFixedList(perMonthList));
			resultMap.put("periodData", periodList);
			resultMap.put("chainData", chainList);
			resultMap.put("totalPayData", toFixedList(tatalPayList));// 总额列表
			resultMap.put("perMonthPeriodData", perMonthPeriodList);// 预拨同比列表
			resultMap.put("perMonthChainData", perMonthChainList);// 预拨环比列表
		}
		return resultMap;
	}

	public BigDecimal filterNullValue(BigDecimal value) {
		if (null == value)
			return new BigDecimal(0);
		else
			return value;
	}

	public Map<String, Integer> createOrderList(String startDate, String endDate) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		Calendar date = Calendar.getInstance();
		SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM");
		int length = 0;
		try {
			if (StringUtils.hasText(startDate) && StringUtils.hasText(endDate)) {
				date.setTime(sft.parse(startDate));
				String data = "";
				if (sft.parse(startDate).getTime() <= sft.parse(endDate).getTime()) {
					do {
						data = sft.format(date.getTime());
						map.put(data, length);
						length++;
						date.add(Calendar.MONTH, 1);
					} while (!data.equals(endDate));
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return map;
	}

	public List<BigDecimal> createSetListValue(int listSize, BigDecimal value) {
		List<BigDecimal> list = new ArrayList<BigDecimal>();
		for (int i = 0; i < listSize; i++) {
			list.add(i, value);
		}
		return list;
	}

	/**
	 * 得到同比或环比日期
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	public String getDate(String year, String month, String flag) {
		Calendar calender = Calendar.getInstance();
		calender.set(Calendar.YEAR, Integer.parseInt(year));
		calender.set(Calendar.MONTH, Integer.parseInt(month) - 1);
		if (flag.equals("1"))// 环比
			calender.add(Calendar.YEAR, -1);
		else
			calender.add(Calendar.MONTH, -1);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
		return format.format(calender.getTime());
	}

	/**
	 * 根据kpicode和月份获取当月值
	 * 
	 * @param sociationCode统筹区
	 * @param kpiTime时间
	 * @param period统计期间维度
	 * @param visitType就诊类型
	 * @param kpiCode指标代码
	 * @return
	 */
	public KpiSociationDTO getCurMonthVal(String sociationCode, String kpiTime, String period, String visitType,
			String kpiCode) {
		KpiConditionDTO conditionDTO = new KpiConditionDTO();
		conditionDTO.setSociationCode(sociationCode);
		conditionDTO.setKpiTime(kpiTime);
		conditionDTO.setPeriod(period);
		conditionDTO.setVisitType(visitType);
		conditionDTO.setKpiCode(kpiCode);
		List<KpiSociationDTO> dataList = kpiCoreDataService.queryKpiBySoc(conditionDTO);
		if (dataList.size() > 0) {
			return dataList.get(0);
		} else {
			return null;
		}

	}

	/**
	 * 元转为万
	 * 
	 * @return
	 */
	public BigDecimal yuanToBillion(BigDecimal yuan) {
		return yuan.divide(new BigDecimal(10000), 2, BigDecimal.ROUND_HALF_UP);
	}

	/**
	 * 点击累计时累加
	 * 
	 * @param list
	 * @return
	 */
	public List<BigDecimal> totalList(List<BigDecimal> list) {
		List<BigDecimal> returnList = new ArrayList<BigDecimal>();
		BigDecimal total = new BigDecimal(0);
		for (BigDecimal bigDecimal : list) {
			total = total.add(bigDecimal);
			returnList.add(total);
		}
		return toFixedList(returnList);
	}

	/**
	 * 转换成万为单位并且保留两位小数
	 * 
	 * @param list
	 */
	private List<BigDecimal> toFixedList(List<BigDecimal> list) {
		List<BigDecimal> returnList = new ArrayList<BigDecimal>();
		for (BigDecimal bigDecimal : list) {
			returnList.add(yuanToBillion(bigDecimal));
		}
		return returnList;
	}

	@Override
	public Map<String, Object> getFeeAnalysis(String startDate, String endDate, String type, String regionCode,
			String visitType) {

		KpiConditionDTO conditionDTO = new KpiConditionDTO();
		List<String> kpiCodeList = new ArrayList<String>();
		kpiCodeList.add("KPI0001");// 费用总额
		kpiCodeList.add("KPI0011");// 自负费用
		kpiCodeList.add("KPI0012");// 自负比例
		String visitTypeParam = Constants.visitType_all_value;
		if (Constants.visitType_hosp.equals(visitType)) {
			visitTypeParam = Constants.visitType_hosp_value;
		} else if (Constants.visitType_out.equals(visitType)) {
			visitTypeParam = Constants.visitType_out_value;
		} else {
			visitTypeParam = Constants.visitType_all_value;
		}
		conditionDTO.setVisitType(visitTypeParam);
		conditionDTO.setPeriod("13");
		conditionDTO.setKpiTimeStart(startDate);
		conditionDTO.setKpiTimeEnd(endDate);
		conditionDTO.setKpiCodeList(kpiCodeList);
		conditionDTO.setSociationCode(regionCode);
		List<KpiSociationDTO> dataList = kpiCoreDataService.queryKpiBySoc(conditionDTO);
		String kpiCode = "";
		String kpiTime = "";
		BigDecimal kpiValue = null;

		Map<String, Integer> orderMap = createOrderList(startDate, endDate);
		List<BigDecimal> sumFeeList = createSetListValue(orderMap.size(), new BigDecimal(0));
		List<BigDecimal> selFeeList = createSetListValue(orderMap.size(), new BigDecimal(0));
		List<BigDecimal> selFeeRatList = createSetListValue(orderMap.size(), new BigDecimal(0));
		for (KpiSociationDTO kpiSociationDTO : dataList) {
			kpiCode = kpiSociationDTO.getKpiCode();
			kpiValue = kpiSociationDTO.getKpiValue();
			kpiTime = kpiSociationDTO.getKpiTime();
			// 医疗费用总额
			if (kpiCode.equals("KPI0001")) {
				sumFeeList.set(orderMap.get(kpiTime), yuanToBillion(filterNullValue(kpiValue)));
			}
			// 自负费用
			if (kpiCode.equals("KPI0011")) {
				selFeeList.set(orderMap.get(kpiTime), yuanToBillion(filterNullValue(kpiValue)));
			}
			// 自负比例
			if (kpiCode.equals("KPI0012")) {
				selFeeRatList.set(orderMap.get(kpiTime), filterNullValue(kpiValue));
			}
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 自付费用当月
		KpiSociationDTO selfFeeDto = getCurMonthVal(regionCode, endDate, "13", visitTypeParam, "KPI0011");
		if (null != selfFeeDto) {
			resultMap.put("curMonthValFee", yuanToBillion(selfFeeDto.getKpiValue()));
			resultMap.put("compSamePeriodFee", selfFeeDto.getPeriodRatio());// 同比
			resultMap.put("compChainFee", selfFeeDto.getChainRatio());// 环比
		} else {
			resultMap.put("curMonthValFee", "0.00");
			resultMap.put("compSamePeriodFee", "0.00");// 同比
			resultMap.put("compChainFee", "0.00");// 环比
		}
		// 自付比例当月
		KpiSociationDTO selfFeeRateDto = getCurMonthVal(regionCode, endDate, "13", visitTypeParam, "KPI0012");
		if (null != selfFeeRateDto) {
			resultMap.put("curMonthValRat", selfFeeRateDto.getKpiValue());
			resultMap.put("compSamePeriodRat", selfFeeRateDto.getPeriodRatio());
			resultMap.put("compChainRat", selfFeeRateDto.getChainRatio());
		} else {
			resultMap.put("curMonthValRat", "0.00");
			resultMap.put("compSamePeriodRat", "0.00");
			resultMap.put("compChainRat", "0.00");
		}
		resultMap.put("sumFeeData", sumFeeList);// 费用总额
		resultMap.put("selFeeData", selFeeList);// 自付费用
		resultMap.put("selFeeRat", selFeeRatList);// 自付比例
		return resultMap;
	}

	@Override
	public List<BigDecimal> getRealPayData(String year, String startMonth, String endMonth, String type) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<BigDecimal> getPerMonthData(String year, String startMonth, String endMonth, String type) {
		// TODO Auto-generated method stub
		return null;
	}

}
